const OrderService = require('@services/order/order.service');
const ApiResponse = require('@shared/utils/response');

class OrderController {
  /**
   * 获取订单列表
   */
  async getOrders(req, res) {
    try {
      const result = await OrderService.getOrders(req.query);
      
      // 确保返回格式统一，包含list和total字段
      const responseData = {
        list: result.list || [],
        total: result.total || 0,
        page: result.page || 1,
        pageSize: result.pageSize || 10
      };
      
      return res.json(ApiResponse.success(responseData, '获取成功'));
    } catch (error) {
      return res.json(ApiResponse.error(error.message || '获取订单列表失败'));
    }
  }

  /**
   * 获取订单详情
   */
  async getOrder(req, res) {
    try {
      const { id } = req.params;
      if (!id) {
        return res.json(ApiResponse.fail('订单ID不能为空'));
      }

      const result = await OrderService.getOrderById(id);
      if (!result || !result.list || result.list.length === 0) {
        return res.json(ApiResponse.fail('订单不存在'));
      }
      
      return res.json(ApiResponse.success(result.list[0], '获取成功'));
    } catch (error) {
      return res.json(ApiResponse.error(error.message || '获取订单详情失败'));
    }
  }

  /**
   * 更新订单状态
   */
  async updateOrderStatus(req, res) {
    try {
      const { id } = req.params;
      const { status, expressCompany, expressNo } = req.body;
      
      if (!id) {
        return res.json(ApiResponse.fail('订单ID不能为空'));
      }
      
      if (status === undefined) {
        return res.json(ApiResponse.fail('订单状态不能为空'));
      }

      // 发货时需要快递信息
      if (status === 2) {
        if (!expressCompany) {
          return res.json(ApiResponse.fail('快递公司不能为空'));
        }
        if (!expressNo) {
          return res.json(ApiResponse.fail('快递单号不能为空'));
        }
      }

      const updateData = { status };
      
      // 添加发货相关信息
      if (status === 2) {
        updateData.expressCompany = expressCompany;
        updateData.expressNo = expressNo;
        updateData.deliveryTime = new Date();
      }
      
      // 订单完成
      if (status === 3) {
        updateData.receiveTime = new Date();
      }
      
      // 订单取消
      if (status === 4) {
        updateData.cancelReason = req.body.cancelReason || '管理员取消';
      }

      const result = await OrderService.updateOrder(id, updateData);
      
      return res.json(ApiResponse.success(result, '更新成功'));
    } catch (error) {
      return res.json(ApiResponse.error(error.message || '更新订单状态失败'));
    }
  }

  /**
   * 批量发货
   */
  async batchShip(req, res) {
    try {
      const { orders } = req.body;
      
      if (!orders || !Array.isArray(orders) || orders.length === 0) {
        return res.json(ApiResponse.fail('订单数据不能为空'));
      }

      const results = await OrderService.batchUpdateOrders(orders);
      
      return res.json(ApiResponse.success(results, '批量发货成功'));
    } catch (error) {
      return res.json(ApiResponse.error(error.message || '批量发货失败'));
    }
  }

  /**
   * 获取订单统计
   */
  async getOrderStats(req, res) {
    try {
      const { startTime, endTime } = req.query;
      const stats = await OrderService.getOrderStats(startTime, endTime);
      
      return res.json(ApiResponse.success(stats, '获取成功'));
    } catch (error) {
      return res.json(ApiResponse.error(error.message || '获取订单统计失败'));
    }
  }
}

module.exports = new OrderController(); 